package com.monster.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
/**
 * 用户收藏DaoImpl
 * @author lym
 */
public class UserLikeDaoImpl extends BaseDao implements UserLikeDao {
    ResultSet rs = null;
    int result = 0;
    //查询某收藏是否存在
    /**
     * 通过用户id,收藏类型，收藏对应的ID
     * @param userId
     * @param likeType
     * @param likeId
     * @return 如果存在就返回1
     */
    @Override
    public int getLikeCountByID(Long userId, int likeType, Long likeId) {
        Object[] params = {userId,likeType,likeId};
        String sql = "SELECT COUNT(1) FROM user_like WHERE user_id = ? AND like_type = ? AND like_id = ?";
        rs = this.executeQuery(sql, params);
        result = 0;
        try {
            while (rs.next()) {
              result = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }
    //添加收藏
    /**
     * 通过用户id,收藏类型，收藏对应的ID
     * @param userId
     * @param likeType
     * @param likeId
     * @return 添加成功返回1
     */
    @Override
    public int addLikeByID(Long userId, int likeType, Long likeId) {
        result = 0;
        Object[] params = {userId,likeType,likeId};
        String sql = "INSERT INTO user_like VALUES (DEFAULT,?,?,?)";
        result = this.executeUpdate(sql,params);
        return result;
    }
    //取消收藏
    /**
     * 通过用户id,收藏类型，收藏对应的ID
     * @param userId
     * @param likeType
     * @param likeId
     * @return 添加成功返回1
     */
    @Override
    public int deleteLikeById(Long userId, int likeType, Long likeId) {
        result = 0;
        Object[] params = {userId,likeType,likeId};
        String sql = "DELETE FROM user_like WHERE user_id = ? AND like_type = ? AND like_id = ?";
        result = this.executeUpdate(sql,params);
        return result;
    }
}
